package com.example.gameapplication.activity;

import android.animation.Animator;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.gameapplication.R;
import com.example.gameapplication.bean.ScoreDataBean;
import com.example.gameapplication.database.DataUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class GameOverDialogActivity extends AppCompatActivity {
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_right)
    TextView tvRight;
    private Unbinder bind;
    private View inflate;
    private Animator animator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflate = LayoutInflater.from(this).inflate(R.layout.activity_game_over_dialog, null);
        setContentView(inflate);
        bind = ButterKnife.bind(this);
        //获取屏幕参数
        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
//获取屏幕宽高
        int width = dm.widthPixels;
        lp.width = DataUtils.getGameViewWidth() / 20 * 19;
        lp.height = DataUtils.getGameViewWidth() * 4 / 5;
//设置屏幕参数则activity的宽高也改变了
        win.setAttributes(lp);
        win.setGravity(Gravity.CENTER);
        setFinishOnTouchOutside(false);
        saveData();
    }

    private void saveData() {
        List<ScoreDataBean> scoreDataBeans;
        MMKV mmkv = MMKV.defaultMMKV();
        String data = mmkv.decodeString("data");
        if (!TextUtils.isEmpty(data)) {
            scoreDataBeans = new Gson().fromJson(data, new TypeToken<List<ScoreDataBean>>() {
            }.getType());
        }else {
            scoreDataBeans=new ArrayList<>();
        }
        ScoreDataBean scoreDataBean = new ScoreDataBean();
        scoreDataBean.setScore(DataUtils.getAllScore());
        scoreDataBean.setBoss(DataUtils.getBoss_sum_kill());
        scoreDataBean.setBig(DataUtils.getBig_sum_kill());
        scoreDataBean.setMiddle(DataUtils.getMiddle_sum_kill());
        scoreDataBean.setSmall(DataUtils.getSmall_sum_kill());
        scoreDataBeans.add(scoreDataBean);
        mmkv.encode("data",new Gson().toJson(scoreDataBeans));
    }

    @Override
    protected void onResume() {
        super.onResume();
        inflate.post(new Runnable() {
            @Override
            public void run() {
                animator = ViewAnimationUtils.createCircularReveal(inflate, 0, 0, 0, getResources().getDisplayMetrics().heightPixels).setDuration(1000);
                inflate.setVisibility(View.VISIBLE);
                animator.start();
            }
        });
    }

    @OnClick({R.id.tv_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                startActivity(WelComeActivity.class);
                MainActivity.mainActivity.finish();
                finish();
                break;
            case R.id.tv_right:
                startActivity(RecordActivity.class);
                MainActivity.mainActivity.finish();
                finish();
                break;
        }
    }

    private void startActivity(Class activity) {
        startActivity(new Intent(this, activity));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bind != null) bind.unbind();
    }
}
